import menus from '../routes/menus'
import { deepClone } from '../utils'

// 扁平化目录
function toFlatMenus (arr) {
  const newArr = []
  
  function loop (arr, parent) {
    for (const i in arr) {
      const item = arr[i]
      item.$parent = parent
      newArr.push(item)

      if (item.children) {
        loop(item.children, item)
      }
    }
  }

  loop(arr, null)

  return newArr
}

const state = {
  menus
}

const mutations = {}

const actions = {}

const getters = {
  flatMenus (state, getters, rootState, rootGetters) {
    return toFlatMenus(deepClone(state.menus))
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
